package test;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

/**
 * 基于 Spring AOP API 构建的一个非常简单的 Spring AOP的环境，用以源码剖析：CglibAopProxy。
 * 见：https://blog.csdn.net/f641385712/article/details/88952482
 *
 * @author ME
 * @date   2020/4/25
 */
public class SpringAopApiCglib {

    public static void main(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory(new Demo());
        proxyFactory.addAdvice( new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args1, Object target) throws Throwable {
                System.out.println("你被拦截了：方法名为：" + method.getName() + " 参数为--" + Arrays.asList(args1));
            }
        });

		Demo demo = (Demo) proxyFactory.getProxy();
		//你被拦截了：方法名为：hello 参数为--[]
		//this demo show
		demo.hello();
    }

	// 不实现接口, 就会用CGLIB去代理
    static class Demo {
        public void hello() {
            System.out.println("demo say hello~~");
        }
    }

}
